/*
 * This file is part of PDBEdit.
 * 
 * PDBEdit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PDBEdit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDBEdit.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 31/08/2010
 * Copyright (c) Guerfyld 2010. All rights reserved.
 * http://......
 */
package pdbfile;

/**
 * The PDBFloat class manage PilotDB Float field
 *
 * @author guerfyld
 * @version 1.0
 */
public class PDBFloat implements PDBType {
	private double dblVal;

	/**
	 * Constructor
	 *
	 * @param val	PilotDB Float field raw value
	 */
	public PDBFloat(long val) {
		setValue(val);
	}

	/**
	 * Constructor
	 *
	 * @param stm	Stream to read
	 * @param pos	Start read position in stream
	 */
	public PDBFloat(PDBInputStream stm, long pos) throws Exception {
		setValue(stm.readValue(pos, 8));
	}

	/**
	 * Set PilotDB Float field raw value
	 *
	 * @param val	pilotDB Float field raw value
	 */
	public void setValue(long val) {
		dblVal = Double.longBitsToDouble(val);
	}

	/**
	 * Set PilotDB Float field value
	 *
	 * @param val	double field value
	 */
	public void setValue(double val) {
		dblVal = val;
	}

	/**
	 * Get PilotDB Float field raw value
	 *
	 * @return	pilotDB Float field raw value
	 */
	public long getValue() {
		return Double.doubleToLongBits(dblVal);
	}

	/**
	 * Compares two PDBFloat objects.
	 * The result is a negative integer if this PDBFloat object precedes the argument PDBFloat.
	 * The result is a positive integer if this PDBFloat object follows the argument PDBFloat.
	 * The result is zero if the PDBFloat are equal.
	 *
	 * @param val	The PDBFloat to be compared
	 * @return	Result value
	 */
	public int compareTo(PDBType val) throws IllegalArgumentException {
		if (val instanceof PDBFloat) {
			if(dblVal > ((PDBFloat)val).dblVal)
				return 1;
			else if(dblVal == ((PDBFloat)val).dblVal)
				return 0;
			else
				return -1;
		}
		else
			throw new IllegalArgumentException();
	}

	/**
	 * Returns a string representation of the object.
	 *
	 * @return	a string representation of the object.
	 */
	public String toString() {
		return Double.toString(dblVal);
	}

	/**
	 * Returns length of raw data
	 */
	public long length() {
		return 8;
	}

	/**
	 * Clone object
	 * @return	New object
	 */
	public PDBType clone() {
		return new PDBFloat(getValue());
	}

	/**
	 * Write fields to stream
	 *
	 * &param stm	Stream to write
	 */
	public void write(PDBOutputStream stm) throws Exception {
		stm.writeValue(getValue(), 8);
	}
}